package com.tea.zxql.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @ClassDescription: 验证码控制器，用于生成和管理图形验证码
 * @ClassName: CaptchaController
 * @JdkVersion: 17
 * @Author: HYC
 * @Created: 2025/7/18 10:51
 * @Version 1.0
 */

@Tag(name = "验证码管理")
@RestController
public class CaptchaController {


    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 获取验证码图片
     * 生成指定尺寸的图形验证码，将验证码文本存储到Redis中，并返回Base64编码的图片数据
     *
     * @param uuid 验证码的唯一标识符，用于关联验证码文本和客户端请求
     * @return SaResult 包含Base64编码的验证码图片数据的响应结果
     */
    @Operation(summary = "获取验证码图片")
    @GetMapping("/getVerify/{uuid}")
    public SaResult getVerify(@PathVariable String uuid) {
        System.out.println(uuid);
        // 创建验证码
        ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
        redisTemplate.opsForValue().set(uuid, shearCaptcha.getCode(), 60, TimeUnit.SECONDS);
        String base64String = "";
        try {
            base64String = "data:image/png;base64," + shearCaptcha.getImageBase64();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SaResult.data(base64String);
    }
}

